<?
global $frm, $smarty, $settings, $group;
$display = $frm ['display'];
$action = $frm ['aAction'];


/**
 * Dieu khien hien thi trang add_program
 * @param unknown_type $groupId
 * @param unknown_type $batch
 * @param unknown_type $paymentType
 * @param unknown_type $emailContact
 * @param unknown_type $errors
 */
function displayAddProgram2Page($groupId, $batch, $paymentType, $emailContact, $errors) {
	global $sqlClass, $smarty;
	$smarty->assign ( 'group_id', $groupId );
	$smarty->assign ( 'group_name', $sqlClass->getGroupNameById ( $groupId ) );
	$smarty->assign ( 'batch', $batch );
	$smarty->assign ( 'payment_type', $paymentType );
	$smarty->assign ( 'email', $emailContact );
	$smarty->assign ( 'payments', CommonClass::getPayments () );
	$smarty->assign ( 'errors', $errors );
	$smarty->display ( 'AddProgram.tpl.tpl' );
}
function notify_about_addition($row) {
	global $settings;
	$row ['withdrawal_type'] = ($row ['withdrawal_type'] == 1 ? 'Manual' : ($row ['withdrawal_type'] == 2 ? 'Instant' : 'Automatic'));
	send_mail ( 'listing_added', $settings ['admin_email'], $settings ['system_email'], $settings ['system_email_password'], $row );
}


if (CommonClass::nullOrBlank ( $action )) {
	if ($display == 'add') {
		$payments = CommonClass::getPayments ();
		$smarty->assign ( 'payments', $payments );
		$smarty->display ( 'adv/AddProgram.tpl' );
		exit ();
	} else {
		$smarty->display ( 'adv/AddProgramRulesConfirmRules.tpl' );
		exit ();
	}
}

if($action == 'add'){
	if (!$frm['name']){
		array_push ($errors, array ('name' => 'Please enter your Site Name'));
	}
	if (!$frm['url']){
		array_push ($errors, array ('name' => 'Please enter your Site URL'));
	}
	if (!$frm['email']){
		array_push ($errors, array ('name' => 'Please enter your Contact E-mail'));
	}elseif (!preg_match ('/^[^\\@]+\\@[^\\@]+\\.\\w{2,4}$/', $frm['email'])){
		array_push ($errors, array ('name' => 'Please enter valid Contact E-mail'));
	}
	
	//truong hop co loi thi hien thi tra ve client 
	if ($errors) {
		displayAddProgram2Page($frm ['group_id'], $frm ['batch'], $frm['payment_type'], $frm ['email'], $errors);
		exit();
	}
	
	//truong hop khong co loi thi insert du lieu vao hl_listings
	$inserts = array ();
	array_push ( $inserts, 'name = \'' . quote ( $frm ['name'] ) . '\'' );
	array_push ( $inserts, 'group_id = \'1\'' );
	array_push ( $inserts, 'status = \''.GlobalConst::$PROGRAM_WATING_APPROVAL.'\'' );
	array_push ( $inserts, 'hyip_status = \'2\'' );
	array_push ( $inserts, 'url = \'' . quote ( $frm ['url'] ) . '\'' );
	array_push ( $inserts, 'percents = \'' . quote ( $frm ['percents'] ) . '\'' );
	array_push ( $inserts, 'min_spend = \'' . quote ( $frm ['min_spend'] ) . '\'' );
	array_push ( $inserts, 'max_spend = \'' . quote ( $frm ['max_spend'] ) . '\'' );
	array_push ( $inserts, 'referral = \'' . quote ( $frm ['referral'] ) . '\'' );
	array_push ( $inserts, 'withdrawal_type = \'' . intval ( $frm ['withdrawal_type'] ) . '\'' );
	array_push ( $inserts, 'email = \'' . quote ( $frm ['email'] ) . '\'' );
	array_push ( $inserts, 'support_email = \'' . quote ( $frm ['support_email'] ) . '\'' );
	array_push ( $inserts, 'support_form = \'' . quote ( $frm ['support_form'] ) . '\'' );
	array_push ( $inserts, 'support_forum = \'' . quote ( $frm ['support_forum'] ) . '\'' );
	array_push ( $inserts, 'support_phone = \'' . quote ( $frm ['support_phone'] ) . '\'' );
	array_push ( $inserts, 'support_aim = \'' . quote ( $frm ['support_aim'] ) . '\'' );
	array_push ( $inserts, 'date_added = now()' );
	array_push ( $inserts, 'date_closed = \'0000-00-00\'' );
	array_push ( $inserts, 'expiration = \'' . abs ( intval ( $group ['expiration'] ) ) . '\'' );
	$add_payments = array ();
	$paymentEcurrent =  CommonClass::getPaymentsEcurrentKey();
	if ($frm ['payments']) {
		foreach ( array_keys ( $frm ['payments'] ) as $paysystem ) {
			if (in_array ( $paysystem, array_keys($paymentEcurrent) )) {
				array_push ( $add_payments, $paysystem );
				continue;
			}
		}
	}
	
	$frm ['pay_systems'] = join ( ',', $add_payments );
	array_push ( $inserts, 'pay_systems = \'' . quote ( $frm ['pay_systems'] ) . '\'' );
	array_push ( $inserts, 'description = \'' . quote ( $frm ['description'] ) . '\'' );
	array_push ( $inserts, 'date_updated = now()' );
	$confirm_code = gen_confirm_code ( 10 );
	if ($settings ['listing_confirmation_require']) {
		array_push ( $inserts, 'confirm = \'' . $confirm_code . '\'' );
	}
	
	$q = 'insert into '.Tables::$HL_LISTINGS.' set ' . join ( ',', $inserts );
	if (! ($sth = mysql_query ( $q ))) {
		header ( "Location: ?a=error" );
		exit ();
	}
	//id cua program vua insert
	$program_id = mysql_insert_id();
		
	$info ['listing_name'] = $frm ['name'];
	$frm ['date_added'] = date ( 'Y-m-d h:i:s' );
	$frm ['group'] = $frm ['group_name'];
	$frm ['id'] = $program_id;
	
	//thong bao toi add min moi co program duoc add
	if ($settings ['notify_about_addition']) {
		notify_about_addition ( $frm );
	}
	//thong bao toi email contac
	send_mail ( 'listing_approve', $frm ['email'], $settings ['system_email'], $settings ['system_email_password'], $frm );
	
	
	$smarty->assign ('say', 'add_program_sucessfull');
	$smarty->assign ('program_id', $program_id);
	$smarty->assign ('email', $frm ['email']);
	$smarty->display ('notify_user.tpl');
	exit ();
}
?>
